home *** CD-ROM | disk | FTP | other *** search
/ One Click 14 / OneClick14.iso / Ferramentas / Convert XLS to Pdf / xls2pdf_setup.exe / {app} / lib / pdf_base.ps < prev    next >
Encoding:
Text File  |  2002-11-02  |  23.2 KB  |  694 lines

  1. %    Copyright (C) 1994, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: pdf_base.ps,v 1.30 2002/11/01 20:00:12 alexcher Exp $
  14. % pdf_base.ps
  15. % Basic parser for PDF reader.
  16.  
  17. % This handles basic parsing of the file (including the trailer
  18. % and cross-reference table), as well as objects, object references,
  19. % streams, and name/number trees; it doesn't include any facilities for
  20. % making marks on the page.
  21.  
  22. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  23. .currentglobal true .setglobal
  24. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  25. pdfdict begin
  26.  
  27. % Define the name interpretation dictionary for reading values.
  28. /valueopdict mark
  29.   (<<) cvn { mark } bind    % don't push an actual mark!
  30.   (>>) cvn { { .dicttomark } stopped {
  31.       (   **** File has an unbalanced >> \(close dictionary\).\n)
  32.       pdfformaterror
  33.     } if
  34.   } bind
  35.   ([) cvn { mark } bind        % ditto
  36.   (]) cvn dup load
  37. %  /true true        % see .pdfexectoken below
  38. %  /false false        % ibid.
  39. %  /null null        % ibid.
  40.   /F dup cvx        % see Objects section below
  41.   /R dup cvx        % see Objects section below
  42.   /stream dup cvx    % see Streams section below
  43. .dicttomark readonly def
  44.  
  45. % ------ Utilities ------ %
  46.  
  47. % Define a scratch string.  The PDF language definition says that
  48. % no line in a PDF file can exceed 255 characters.
  49. /pdfstring 255 string def
  50.  
  51. % Read the previous line of a file.  If we aren't at a line boundary,
  52. % read the line containing the current position.
  53. % Skip any blank lines.
  54. /prevline        % - prevline <startpos> <substring>
  55.  { PDFfile fileposition dup () pdfstring
  56.    2 index 257 sub 0 .max PDFfile exch setfileposition
  57.     {        % Stack: initpos linepos line string
  58.       PDFfile fileposition
  59.       PDFfile 2 index readline pop
  60.       dup length 0 gt
  61.        { 3 2 roll 5 -2 roll pop pop 2 index }
  62.        { pop }
  63.       ifelse
  64.         % Stack: initpos linepos line string startpos
  65.       PDFfile fileposition 5 index ge { exit } if
  66.       pop
  67.     }
  68.    loop pop pop 3 -1 roll pop
  69.  } bind def
  70.  
  71. % Handle the PDF 1.2 #nn escape convention when reading from a file.
  72. % This should eventually be done in C.
  73. /.pdffixname {            % <execname> .pdffixname <execname'>
  74.   PDFversion 1.2 ge {
  75.     dup .namestring (#) search {
  76.       name#escape cvn exch pop
  77.     } {
  78.       pop
  79.     } ifelse
  80.   } if
  81. } bind def
  82. /name#escape            % <post> <(#)> <pre> name#escape <string>
  83. { exch pop
  84.   1 index 2 () /SubFileDecode filter dup (x) readhexstring
  85.         % Stack: post pre stream char t/f
  86.   not { /.pdftoken cvx /syntaxerror signalerror } if
  87.   exch closefile concatstrings
  88.   exch 2 1 index length 2 sub getinterval
  89.   (#) search { name#escape } if concatstrings
  90. } bind def
  91.  
  92. % Execute a file, interpreting its executable names in a given
  93. % dictionary.  The name procedures may do whatever they want
  94. % to the operand stack.
  95. /.pdftokenerror {        % <count> <opdict> <errtoken> .pdftokenerror -
  96.   BXlevel 0 le {
  97.     (   **** Unknown operator: ') pdfformaterror
  98.     dup =string cvs pdfformaterror 
  99.     % Attempt a retry scan of the element after changing to PDFScanRules
  100.     << /PDFScanRules true >> setuserparams
  101.     =string cvs
  102.     token pop exch pop dup type
  103.     dup /integertype eq exch /realtype eq or {
  104.       exch pop exch pop
  105.       (', processed as number, value: ) pdfformaterror
  106.       dup =string cvs pdfformaterror (\n) pdfformaterror
  107.       false     % suppress any stack cleanup
  108.     } {
  109.       % error was non-recoverable with modified scanning rules
  110.       (\n) pdfformaterror
  111.       true
  112.     } ifelse
  113.   } {
  114.     true
  115.   } ifelse
  116.   { % clean up the operand stack if this was non-recoverable
  117.     pop pop count exch sub { pop } repeat    % pop all the operands
  118.   } if
  119. } bind def
  120. /.pdfexectoken {        % <count> <opdict> <exectoken> .pdfexectoken ?
  121.   DEBUG { dup == flush } if
  122.   2 copy .knownget {
  123.     exch pop exch pop exch pop exec
  124.   } {
  125.         % Normally, true, false, and null would appear in opdict
  126.         % and be treated as "operators".  However, there is a
  127.         % special fast case in the PostScript interpreter for names
  128.         % that are defined in, and only in, systemdict and/or
  129.         % userdict: putting these three names in the PDF dictionaries
  130.         % destroys this property for them, slowing down their
  131.         % interpretation in all PostScript code.  Therefore, we
  132.         % check for them explicitly here instead.
  133.     dup dup dup /true eq exch /false eq or exch /null eq or {
  134.       exch pop exch pop //systemdict exch get
  135.     } {
  136.       .pdftokenerror
  137.     } ifelse
  138.   } ifelse
  139. } bind def
  140. /.pdfrun {            % <file> <opdict> .pdfrun -
  141.     % Construct a procedure with the stack depth, file and opdict
  142.     % bound into it.
  143.   1 index cvlit count 2 sub 3 1 roll mark mark 5 2 roll
  144.   {    % Stack: ..operands.. count opdict file
  145.     token {
  146.       dup type /nametype eq {
  147.     dup xcheck {
  148.       .pdfexectoken
  149.     } {
  150.       .pdffixname
  151.       exch pop exch pop DEBUG { dup ==only ( ) print flush } if
  152.     } ifelse
  153.       } {
  154.     exch pop exch pop DEBUG { dup ==only ( ) print flush } if
  155.       } ifelse
  156.     } {
  157.       (%%EOF) cvn cvx .pdfexectoken
  158.     } ifelse
  159.   }
  160.   aload pop .packtomark cvx
  161.   /loop cvx 2 packedarray cvx
  162.   { stopped /PDFsource } aload pop
  163.   PDFsource
  164.   { store { stop } if } aload pop .packtomark cvx
  165.   /PDFsource 3 -1 roll store exec
  166. } bind def
  167.  
  168. % Execute a file, like .pdfrun, for a marking context.
  169. % This temporarily rebinds LocalResources and DefaultMatrix.
  170. /.pdfruncontext {        % <resdict> <file> <opdict> .pdfruncontext -
  171.   /.pdfrun load LocalResources DefaultMatrix
  172.   /LocalResources 7 -1 roll store
  173.   /DefaultMatrix matrix currentmatrix store
  174.   3 .execn
  175.   /DefaultMatrix exch store
  176.   /LocalResources exch store
  177. } bind def
  178.  
  179. % Get the depth of the PDF operand stack.  The caller sets pdfemptycount
  180. % before calling .pdfrun or .pdfruncontext.  It is initially set by
  181. % pdf_main, and is also set by any routine which changes the operand
  182. % stack depth (currently .pdfpaintproc, although there are other callers
  183. % of .pdfrun{context} which have not been checked for opstack depth.
  184. /.pdfcount {        % - .pdfcount <count>
  185.   count pdfemptycount sub
  186. } bind def
  187.  
  188. % ------ File reading ------ %
  189.  
  190. % Read the cross-reference entry for an (unresolved) object.
  191. % The caller must save and restore the PDFfile position if desired.
  192. % For invalid (free) objects, we return 0.
  193. /readxrefentry        % <object#> readxrefentry <objpos>
  194.  { dup Objects exch lget
  195.    PDFfile exch setfileposition
  196.    PDFfile token pop        % object position
  197.    PDFfile token pop        % generation #
  198.    PDFfile token pop        % n or f
  199.    dup /n eq
  200.     { pop 1 add dup 255 gt
  201.        { Generations ltype /stringtype eq
  202.       {        % Convert Generations from a string to an array.
  203.         larray Generations llength lgrowto dup
  204.         0 1 2 index llength 1 sub
  205.          { Generations 1 index lget lput dup
  206.          }
  207.         for pop /Generations exch store
  208.       }
  209.      if
  210.        }
  211.       if
  212.     }
  213.     { /f eq
  214.        { pop 0 }
  215.        { /readxrefentry cvx /syntaxerror signalerror }
  216.       ifelse
  217.     }
  218.    ifelse
  219.         % Stack: obj# objpos 1+gen#
  220.    Generations 4 -1 roll 3 -1 roll lput
  221.  } bind def
  222.  
  223. % ================================ Objects ================================ %
  224.  
  225. % Since we may have more than 64K objects, we have to use a 2-D array to
  226. % hold them (and the parallel Generations structure).
  227. /lshift 9 def
  228. /lnshift lshift neg def
  229. /lsubmask 1 lshift bitshift 1 sub def
  230. /lsublen lsubmask 1 add def
  231. /larray {    % - larray <larray>
  232.   [ [] ]
  233. } bind def
  234. /lstring {    % - lstring <lstring>
  235.   [ () ]
  236. } bind def
  237. /ltype {    % <lseq> type <type>
  238.   0 get type
  239. } bind def
  240. /lget {        % <lseq> <index> lget <value>
  241.   dup //lsubmask and 3 1 roll //lnshift bitshift get exch get
  242. } bind def
  243. /lput {        % <lseq> <index> <value> lput -
  244.   3 1 roll
  245.   dup //lsubmask and 4 1 roll //lnshift bitshift get
  246.   3 1 roll put
  247. } bind def
  248. /llength {    % <lseq> llength <length>
  249.   dup length 1 sub dup //lshift bitshift
  250.   3 1 roll get length add
  251. } bind def
  252. % lgrowto assumes newlength > llength(lseq)
  253. /growto {    % <string/array> <length> growto <string'/array'>
  254.   1 index type /stringtype eq { string } { array } ifelse
  255.   2 copy copy pop exch pop
  256. } bind def
  257. /lgrowto {    % <lseq> <newlength> lgrowto <lseq'>
  258.     dup //lsubmask add //lnshift bitshift dup 3 index length gt {
  259.     % Add more sub-arrays.  Start by completing the last existing one.
  260.         % Stack: lseq newlen newtoplen
  261.     3 -1 roll dup llength 1 sub //lsubmask or 1 add lgrowto
  262.         % Stack: newlen newtoplen lseq
  263.     [ exch aload pop
  264.     counttomark 2 add -1 roll        % newtoplen
  265.     counttomark sub { dup 0 0 getinterval lsublen growto } repeat
  266.     dup 0 0 getinterval ] exch
  267.   } {
  268.     pop
  269.   } ifelse
  270.     % Expand the last sub-array.
  271.   1 sub //lsubmask and 1 add
  272.   exch dup dup length 1 sub 2 copy
  273.         % Stack: newsublen lseq lseq len-1 lseq len-1
  274.   get 5 -1 roll growto put
  275. } bind def
  276. /lforall {    % <lseq> <proc> lforall -
  277.   /forall cvx 2 packedarray cvx forall
  278. } bind def
  279.  
  280. % We keep track of PDF objects using the following PostScript variables:
  281. %
  282. %    Generations (lstring): Generations[N] holds 1+ the current
  283. %        generation number for object number N.  (As far as we can tell,
  284. %        this is needed only for error checking.)  For free objects,
  285. %        Generations[N] is 0.
  286. %
  287. %    Objects (larray): If object N is loaded, Objects[N] is the actual
  288. %        object; otherwise, Objects[N] is an executable integer giving
  289. %        the file offset of the object's entry in the cross-reference
  290. %        table.
  291. %
  292. %    GlobalObjects (dictionary): If object N has been resolved in
  293. %        global VM, GlobalObjects[N] is the same as Objects[N]
  294. %        (except that GlobalObjects itself is stored in global VM,
  295. %        so the entry will not be deleted at the end of the page).
  296. %
  297. %    IsGlobal (lstring): IsGlobal[N] = 1 iff object N was resolved in
  298. %        global VM.  This is an accelerator to avoid having to do a
  299. %        dictionary lookup in GlobalObjects when resolving every object.
  300.  
  301. % Initialize the PDF object tables.
  302. /initPDFobjects {        % - initPDFobjects -
  303.   /Objects larray def
  304.   /Generations lstring def
  305.   .currentglobal true .setglobal
  306.   /GlobalObjects 20 dict def
  307.   .setglobal
  308.   /IsGlobal lstring def
  309. } bind def
  310.  
  311. % Grow the tables to a specified size.
  312. /growPDFobjects {        % <minsize> growPDFobjects -
  313.   dup Objects llength gt {
  314.     dup Objects exch lgrowto /Objects exch def
  315.   } if
  316.   dup Generations llength gt {
  317.     dup Generations exch lgrowto /Generations exch def
  318.   } if
  319.   dup IsGlobal llength gt {
  320.     dup IsGlobal exch lgrowto /IsGlobal exch def
  321.   } if
  322.   pop
  323. } bind def
  324.  
  325. % We represent an unresolved object reference by a procedure of the form
  326. % {obj# gen# resolveR}.  This is not a possible PDF object, because PDF has
  327. % no way to represent procedures.  Since PDF in fact has no way to represent
  328. % any PostScript object that doesn't evaluate to itself, we can 'force'
  329. % a possibly indirect object painlessly with 'exec'.
  330. % Note that since we represent streams by executable dictionaries
  331. % (see below), we need both an xcheck and a type check to determine
  332. % whether an object has been resolved.
  333. /resolved? {        % <object#> resolved? <value> true
  334.             % <object#> resolved? false
  335.   Objects 1 index lget dup xcheck {
  336.     dup type /integertype eq {
  337.         % Check whether the object is in GlobalObjects.
  338.       pop IsGlobal 1 index lget 0 eq {
  339.     pop false
  340.       } {
  341.         % Update Objects from GlobalObjects
  342.     DEBUG { (%Global=>local: ) print dup == } if
  343.     GlobalObjects 1 index get dup Objects 4 1 roll lput true
  344.       } ifelse
  345.     } {
  346.       exch pop true
  347.     } ifelse
  348.   } {
  349.     exch pop true
  350.   } ifelse
  351. } bind def
  352. /oforce /exec load def
  353. /oget {        % <array> <index> oget <object>
  354.         % <dict> <key> oget <object>
  355.         % Before release 6.20, this procedure stored the resolved
  356.         % object back into the referring slot.  In order to support
  357.         % PDF linearization, we no longer do this.
  358.   get oforce
  359. } bind def
  360. % A null value in a dictionary is equivalent to an omitted key;
  361. % we must check for this specially.
  362. /knownoget {    % <dict> <key> knownoget <value> true
  363.         % <dict> <key> knownoget false
  364.         % See oget above regarding this procedure.
  365.   .knownget {
  366.     oforce dup null eq { pop false } { true } ifelse
  367.   } {
  368.     false
  369.   } ifelse
  370. } bind def
  371.  
  372. % PDF 1.1 defines a 'foreign file reference', but not its meaning.
  373. % Per the specification, we convert these to nulls.
  374. /F {        % <file#> <object#> <generation#> F <object>
  375.         % Some PDF 1.1 files use F as a synonym for f!
  376.    .pdfcount 3 lt { f } { pop pop pop null } ifelse
  377. } bind def
  378.  
  379. /checkgeneration {  % <object#> <generation#> checkgeneration <object#> <OK>
  380.   Generations 2 index lget 1 sub 1 index eq {
  381.     pop true
  382.   } {
  383.     QUIET not {
  384.       Generations 2 index lget 0 eq {
  385.     (   **** Warning: reference to free object: )
  386.       } {
  387.     (   **** Warning: wrong generation: )
  388.       } ifelse
  389.       2 index =string cvs concatstrings ( ) concatstrings
  390.       exch =string cvs concatstrings ( R\n) concatstrings
  391.       pdfformaterror
  392.     } {
  393.       pop
  394.     } ifelse false
  395.   } ifelse
  396. } bind def
  397. /R {        % <object#> <generation#> R <object>
  398.   /resolveR cvx 3 packedarray cvx
  399. } bind def
  400.  
  401. % If we encounter an object definition while reading sequentially,
  402. % we just store it away and keep going.
  403. /objopdict mark
  404.   valueopdict { } forall
  405.   /endobj dup cvx
  406. .dicttomark readonly def
  407. /obj {            % <object#> <generation#> obj <object>
  408.   PDFfile objopdict .pdfrun
  409. } bind def
  410. /endobj {        % <object#> <generation#> <object> endobj <object>
  411.   3 1 roll
  412.         % Read the xref entry if we haven't yet done so.
  413.         % This is only needed for generation # checking.
  414.   1 index resolved? {
  415.     pop
  416.   } {
  417.     PDFfile fileposition
  418.     2 index readxrefentry pop
  419.     PDFoffset add PDFfile exch setfileposition
  420.   } ifelse
  421.   checkgeneration {
  422.         % The only global objects we bother to save are
  423.         % (resource) dictionaries.
  424.     1 index dup gcheck exch type /dicttype eq and {
  425.       DEBUG { (%Local=>global: ) print dup == } if
  426.       GlobalObjects 1 index 3 index put
  427.       IsGlobal 1 index 1 put
  428.     } if
  429.     Objects exch 2 index lput
  430.   } {
  431.     pop pop null
  432.   } ifelse
  433. } bind def
  434.  
  435. % When resolving an object reference, we stop at the endobj.
  436. /resolveopdict mark
  437.   valueopdict { } forall
  438.   /endobj { endobj exit } bind
  439.                 % OmniForm generates PDF file with endobj missing in some
  440.                 % objects. AR ignores this. So we have to do it too.
  441.   /obj { pop pop endobj exit } bind
  442. .dicttomark readonly def
  443. /resolveR {        % <object#> <generation#> resolveR <object>
  444.   DEBUG { (%Resolving: ) print 2 copy 2 array astore == } if
  445.   1 index resolved? {
  446.     exch pop exch pop
  447.   } {
  448.     PDFfile fileposition 3 1 roll
  449.     1 index readxrefentry
  450.     3 1 roll checkgeneration {
  451.             % Stack: savepos objpos obj#
  452.      exch PDFoffset add PDFfile exch setfileposition
  453.      PDFfile token pop 2 copy ne
  454.       { (   **** Unrecoverable error in xref!\n) pdfformaterror
  455.         /resolveR cvx /rangecheck signalerror
  456.       }
  457.      if pop PDFfile token pop
  458.      PDFfile token pop /obj ne
  459.       { (   **** Unrecoverable error in xref!\n) pdfformaterror
  460.         /resolveR cvx /rangecheck signalerror
  461.       }
  462.      if
  463.      pdf_run_resolve    % PDFfile resolveopdict .pdfrun
  464.     }
  465.     {        % Don't cache if the generation # is wrong.
  466.      pop pop null
  467.     } ifelse
  468.     exch PDFfile exch setfileposition
  469.   } ifelse
  470. } bind def      
  471.  
  472. % ================================ Streams ================================ %
  473.  
  474. % We represent a stream by an executable dictionary that contains,
  475. % in addition to the contents of the original stream dictionary:
  476. %    /File - the file or string where the stream contents are stored,
  477. %      if the stream is not an external one.
  478. %    /FilePosition - iff File is a file, the position in the file
  479. %      where the contents start.
  480. %    /StreamKey - the key used to decrypt this stream, if any.
  481. % We do the real work of constructing the data stream only when the
  482. % contents are needed.
  483.  
  484. % Construct a stream.  The length is not reliable in the face of
  485. % different end-of-line conventions, but it's all we've got.
  486. %
  487. % PDF files are inconsistent about what may fall between the 'stream' keyword
  488. % and the actual stream data, and it appears that no one algorithm can
  489. % detect this reliably.  We used to try to guess whether the file included
  490. % extraneous \r and/or \n characters, but we no longer attempt to do so,
  491. % especially since the PDF 1.2 specification states flatly that the only
  492. % legal terminators following the 'stream' keyword are \n or \r\n, both of
  493. % which are properly skipped and discarded by the token operator.
  494. % Unfortunately, this doesn't account for other whitespace characters that
  495. % may have preceded the EOL, such as spaces or tabs. Thus we back up one
  496. % character and scan until we find the \n terminator.
  497. /stream {    % <dict> stream <modified_dict>
  498.   dup /Length oget 0 eq {
  499.     dup /Filter undef    % don't confuse any filters that require data
  500.   } if
  501.   dup /F known dup PDFsource PDFfile eq or {
  502.     not {
  503.       dup /File PDFfile put
  504.       % make sure that we are just past the EOL \n character
  505.       PDFfile dup fileposition 1 sub setfileposition    % back up one
  506.       { PDFfile read pop dup 13 eq {
  507.       % If there had been a \n, token would have advanced over it
  508.       % thus, if the terminator was \r, we have a format error!
  509.       (   **** Warning: stream operator not terminated by valid EOL.\n) pdfformaterror
  510.       pop exit    % fileposition is OK (just past the \r).
  511.     } if 
  512.     10 eq { exit } if
  513.       } loop    % scan past \n
  514.       dup /FilePosition PDFfile fileposition put
  515.       DEBUG { (%FilePosition: ) print dup /FilePosition get == } if
  516.     } if
  517.     PDFfile fileposition 1 index /Length oget add
  518.     PDFfile exch setfileposition
  519.   } {
  520.     pop
  521.     % We're already reading from a stream, which we can't reposition.
  522.     % Capture the sub-stream contents in a string.
  523.     dup /Length oget string PDFsource exch readstring
  524.     not {
  525.       (   **** Warning: Unexpected EOF in stream!\n) pdfformaterror
  526.       /stream cvx /rangecheck signalerror
  527.     } if
  528.     1 index exch /File exch put
  529.   } ifelse
  530.   PDFsource {token} stopped {
  531.     pop null
  532.   } {
  533.     not { null } if
  534.   } ifelse
  535.   dup /endobj eq {
  536.     % Another case that Acrobat Reader handles -- 'endobj' without 'endstream'.
  537.     (   **** Warning: stream missing 'endstream'.\n) pdfformaterror
  538.     pop /endstream        % fake a valid endstream
  539.   } if
  540.   /endstream ne { 
  541.     (   **** Warning: stream Length incorrect.\n) pdfformaterror
  542.     exit % exit from .pdfrun now.
  543.   } if
  544.   cvx
  545. } bind def
  546. /endstream {
  547.   exit
  548. } bind def
  549.  
  550. % Contrary to the published PDF (1.3) specification, Acrobat Reader
  551. % accepts abbreviated filter names everywhere, not just for in-line images,
  552. % and some applications (notably htmldoc) rely on this.
  553. /unabbrevfilterdict mark
  554.   /AHx /ASCIIHexDecode  /A85 /ASCII85Decode  /CCF /CCITTFaxDecode
  555.   /DCT /DCTDecode  /Fl /FlateDecode  /LZW /LZWDecode  /RL /RunLengthDecode
  556. .dicttomark readonly def
  557.  
  558. % Extract and apply filters.
  559. /filterparms {        % <dict> <DPkey> <Fkey> filterparms
  560.             %   <dict> <parms> <filternames>
  561.   2 index exch knownoget {
  562.     exch 2 index exch knownoget {
  563.         % Both filters and parameters.
  564.       exch dup type /nametype eq {
  565.     1 array astore exch 1 array astore exch
  566.       } if
  567.     } {
  568.         % Filters, but no parameters.
  569.       null exch
  570.       dup type /nametype eq { 1 array astore } if
  571.     } ifelse
  572.   } {
  573.         % No filters: ignore parameters, if any.
  574.     pop null { }
  575.   } ifelse
  576. } bind def
  577. /filtername {        % <filtername> filtername <filtername'>
  578.   //unabbrevfilterdict 1 index .knownget { exch pop } if
  579. } bind def
  580. /applyfilters {        % <parms> <source> <filternames> applyfilters <stream>
  581.   2 index null eq {
  582.     { filtername filter }
  583.   } {
  584.     {        % Stack: parms stream filtername
  585.       2 index 0 oget dup null eq { pop } { exch } ifelse filtername filter
  586.       exch dup length 1 sub 1 exch getinterval exch
  587.     }
  588.   } ifelse forall exch pop
  589. } bind def
  590.  
  591. % Resolve a stream dictionary to a PostScript stream.
  592. % Streams with no filters require special handling:
  593. %     - Whether we are going to interpret the stream, or If we are just
  594. %       going to read data from them, we impose a SubFileDecode filter
  595. %         that reads just the requisite amount of data.
  596. % Note that, in general, resolving a stream repositions PDFfile.
  597. % Clients must save and restore the position of PDFfile themselves.
  598. /resolvestream {    % <streamdict> <readdata?> resolvestream <stream>
  599.   1 index /F knownoget {
  600.         % This stream is stored on an external file.
  601.     (r) file 3 -1 roll
  602.     /FDecodeParms /FFilter filterparms
  603.         % Stack: readdata? file dict parms filternames
  604.     4 -1 roll exch
  605.     pdf_decrypt_stream
  606.     applyfilters
  607.   } {
  608.     exch dup /FilePosition .knownget {
  609.       1 index /File get exch setfileposition
  610.     } if
  611.         % Stack: readdata? dict
  612.     /DecodeParms /Filter filterparms
  613.         % Stack: readdata? dict parms filternames
  614.     2 index /File get exch
  615.         % Stack: readdata? dict parms file/string filternames
  616.     pdf_decrypt_stream        % add decryption if needed
  617.     dup length 0 eq {
  618.         % All the PDF filters have EOD markers, but in this case
  619.         % there is no specified filter.
  620.       pop exch pop
  621.         % Stack: readdata? dict file/string
  622.       2 index 1 index type /filetype eq or {
  623.               % Use length for any files or reading data from any source.
  624.         1 index /Length knownoget not { 0 } if
  625.       } {
  626.       0       % Otherwise length of 0 for whole string
  627.       } ifelse
  628.       2 index /IDFlag known { pop } { () /SubFileDecode filter } ifelse
  629.     } {
  630.       applyfilters
  631.     } ifelse
  632.   } ifelse
  633.         % Stack: readdata? dict file
  634.   exch pop exch pop
  635. } bind def
  636.  
  637. % ============================ Name/number trees ============================ %
  638.  
  639. /nameoget {        % <nametree> <key> nameoget <obj|null>
  640.   exch /Names exch .treeget
  641. } bind def
  642.  
  643. /numoget {        % <numtree> <key> numoget <obj|null>
  644.   exch /Nums exch .treeget
  645. } bind def
  646.  
  647. /.treeget {        % <key> <leafkey> <tree> .treeget <obj|null>
  648.   dup /Kids knownoget {
  649.     exch pop .branchget
  650.   } {
  651.     exch get .leafget
  652.   } ifelse
  653. } bind def
  654.  
  655. /.branchget {        %  <key> <leafkey> <kids> .branchget <obj|null>
  656.   dup length 0 eq {
  657.     pop pop pop null
  658.   } {
  659.     dup length -1 bitshift 2 copy oget
  660.             % Stack: key leafkey kids mid kids[mid]
  661.     dup /Limits oget aload pop
  662.             % Stack: key leafkey kids mid kids[mid] min max
  663.     6 index lt {
  664.       pop pop
  665.       1 add 1 index length 1 index sub getinterval .branchget
  666.     } {
  667.       5 index gt {
  668.     pop
  669.     0 exch getinterval .branchget
  670.       } {
  671.     exch pop exch pop .treeget
  672.       } ifelse
  673.     } ifelse
  674.   } ifelse
  675. } bind def
  676.  
  677. /.leafget {        % <key> <pairs> .leafget <obj|null>
  678.   dup length 2 eq {
  679.     dup 0 get 2 index eq { 1 oget } { pop null } ifelse
  680.     exch pop
  681.   } {
  682.     dup length -1 bitshift -2 and 2 copy oget
  683.             % Stack: key pairs mid pairs[mid]
  684.     3 index gt { 0 exch } { 1 index length 1 index sub } ifelse
  685.     getinterval .leafget
  686.   } ifelse
  687. } bind def
  688.  
  689. end            % pdfdict
  690. .setglobal
  691.